/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author WenYong
 */
@ManagedBean
@RequestScoped
public class StakeholderAddUnstructuredContentUploadImageManager {

    private String imgUrl;
    private FacesContext ctx = FacesContext.getCurrentInstance();

    /** Creates a new instance of StakeholderAddUnstructuredContentUploadImageManager */
    public StakeholderAddUnstructuredContentUploadImageManager() {
    }

    public void handleImageUpload(FileUploadEvent event) throws AbortProcessingException, NoSuchAlgorithmException {
        try {
            String panel = "";
            if (ctx.getExternalContext().getSessionMap().containsKey("Panel")) {
                panel = ctx.getExternalContext().getSessionMap().get("Panel").toString();
            }
            String fileName = panel;
            String filePath =
                    FacesContext.getCurrentInstance().getExternalContext().getRealPath("images/stakeholder/unstructured");
            System.out.println("filePath is: " + filePath);
            File result = new File(filePath + System.getProperty("file.separator") + fileName);
            System.out.println("File Name: "+fileName);
            FileOutputStream out = new FileOutputStream(result);

            int a;
            int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];

            InputStream is = event.getFile().getInputstream();

            while (true) {
                a = is.read(buffer);

                if (a < 0) {
                    break;
                }

                out.write(buffer, 0, a);
                out.flush();
            }

            out.close();
            is.close();

            imgUrl = "images/stakeholder/unstructured/" + fileName;
            System.out.println("imagefilepath: " + getImgUrl());
            FacesMessage msg = new FacesMessage("Succesful", fileName + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            ctx.getExternalContext().getSessionMap().put("imageName", fileName);
            System.out.println("Image Name Session: " + fileName);
        } catch (IOException e) {
            FacesContext.getCurrentInstance().addMessage("sPortalform", new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "File upload failed with I/O error.", null));
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String getImgUrl() {
        System.out.println(imgUrl);
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        System.out.println("set:" + imgUrl);
        this.imgUrl = imgUrl;
    }

    public void confirmImage() {
        String template = "";
        if (ctx.getExternalContext().getSessionMap().containsKey("TemplateSelected")) {
            template = ctx.getExternalContext().getSessionMap().get("TemplateSelected").toString();
            System.out.println("Templatte: " + template);
        }
        try {
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            if (template.equalsIgnoreCase("1")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateOne.xhtml");
            } else if (template.equalsIgnoreCase("2")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateTwo.xhtml");
            } else if (template.equalsIgnoreCase("3")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateThree.xhtml");
            } else if (template.equalsIgnoreCase("4")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateFour.xhtml");
            } else if (template.equalsIgnoreCase("5")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateFive.xhtml");
            } else if (template.equalsIgnoreCase("6")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateSix.xhtml");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
